package com.export.service;

import com.alibaba.fastjson.JSON;
import com.export.dao.OperateLogDao;
import com.export.dao.ProductCategoryReportDao;
import com.export.entity.DownloadLog;
import com.export.entity.ProductCategoryReport;
import com.export.entity.ProductCategoryReportDetail;
import com.export.entity.ReportParam;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 接口分类报表
 *
 * @author yuanqinglong
 * @since 2021/7/22 9:12
 */
@Service
public class ProductCategoryReportServiceImpl implements IReportService<ReportParam> {


    @Autowired
    private OperateLogDao operateLogDao;


    @Autowired
    private ProductCategoryReportDao productCategoryReportDao;


    /**
     * 导出表
     *
     * @param reportParam 报告参数
     */
//    @Async
    @Override
    public void exportReportTable(ReportParam reportParam) {
        TableNode tableNode = productCategoryReportDao.productCategoryReportExportList(reportParam);
        List<ProductCategoryReport> productCategoryReports = JSON.parseArray(tableNode.toJson(), ProductCategoryReport.class);
        productCategoryReports.forEach(productCategoryReport -> productCategoryReport.processingAttributeValue(reportParam));
        this.saveLogCreateExcelUpload(productCategoryReports, ProductCategoryReport.class, "接口分类报表", reportParam.getOperatorId());
    }

    /**
     * 导出详情列表
     *
     * @param reportParam 报告参数
     */
//    @Async
    @Override
    public void exportDetailTable(ReportParam reportParam) {
        TableNode tableNode = productCategoryReportDao.productCategoryReportExportDetailList(reportParam);
        List<ProductCategoryReportDetail> productCategoryReportDetails = JSON.parseArray(tableNode.toJson(), ProductCategoryReportDetail.class);
        productCategoryReportDetails.forEach(ProductCategoryReportDetail::calculateRate);
        this.saveLogCreateExcelUpload(productCategoryReportDetails, ProductCategoryReportDetail.class, "接口分类报表", reportParam.getOperatorId());
    }



    /**
     * 下载日志
     *
     * @param reportParam 报告参数
     * @param cycleIndex  循环次数
     * @return {@link List<DownloadLog>}
     */
    @Override
    public List<DownloadLog> downloadLog(ReportParam reportParam, int cycleIndex) {
        int offset = (cycleIndex - 1) * LIMIT_SIZES;
        TableNode accountLog = operateLogDao.productCategoryLog(reportParam.getStartDate(), reportParam.getEndDate(), reportParam.getProdTypeFid(), offset, LIMIT_SIZES);
        return JSON.parseArray(accountLog.toJson(), DownloadLog.class);
    }


    /**
     * 下载日志数
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int downloadLogCount(ReportParam reportParam) {
        return operateLogDao.productCategoryLogCount(reportParam.getStartDate(), reportParam.getEndDate(), reportParam.getProdTypeFid()).valueInt();
    }




}
